package com.jshoperxms.controller.mall.backstage.memberrecharge;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.mall.backstage.vo.member.MemberRechargeVo;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.MemberRechargeT;
import com.jshoperxms.service.MemberRechargeTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by sdywcd on 16/10/22.
 * Des:会员充值信息控制器
 */
@Controller
@RequestMapping("/mall/memberrecharge")
public class MemberRechargeTController extends BaseTController implements IBaseCtrl<MemberRechargeT> {

    @Resource
    private MemberRechargeTService memberRechargeTService;

    private Map<String,Object> responsedata=new HashMap<>();

    //充值
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public ResponseEntity save(@RequestBody MemberRechargeVo rq) {
        //检查会员是否有充值中心记录
        if(StringUtils.isBlank(rq.getMemberid())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            MemberRechargeT bean=new MemberRechargeT();
            bean.setMemberid(rq.getMemberid());
            bean.setMembername(rq.getMembername());
            bean.setBalance(rq.getMoney());

            this.memberRechargeTService.updateMemberRechargeTransactional(bean,rq.getType());
            responsedata.put(TagWords.SUCCESS, true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity save(@RequestBody MemberRechargeT rq) {
        return null;
    }

    /**
     * 查询会员的余额
     * @param rq
     * @return
     */
    @RequestMapping(value="/find",method= RequestMethod.POST)
    @Override
    public ResponseEntity find(@RequestBody MemberRechargeT rq) {
        if(StringUtils.isBlank(rq.getMemberid())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            Criterion criterion=Restrictions.eq("memberid",rq.getMemberid());
            MemberRechargeT bean=this.memberRechargeTService.findOneByCriteria(MemberRechargeT.class,criterion);
            if(bean!=null){
                responsedata.put(TagWords.SUCCESS,true);
                responsedata.put(TagWords.BEAN,bean);
                responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
            }
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity del(@RequestBody MemberRechargeT rq) {
        return null;
    }

    @Override
    public ResponseEntity update(@RequestBody MemberRechargeT rq) {
        return null;
    }

    @Override
    public ResponseEntity findAll() {
        return null;
    }

    //查询
    @RequestMapping(value="/findByPage",method= RequestMethod.POST)
    @Override
    public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
        findCustomSearch(searchModel,search,ordervalue,ordertype);
        return Response.success(responsedata);
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        if(searchModel.getIsCustomSearch()==1){
            int currentPage=searchModel.getCalCurrentPage();
            int lineSize = searchModel.getLength();
            Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
            if(StringUtils.isNotBlank(searchModel.getName())){
                criterion=Restrictions.and(criterion).add(Restrictions.like("membername",searchModel.getName(), MatchMode.ANYWHERE));
            }
            if(StringUtils.isNotBlank(search)){
                criterion=Restrictions.and(criterion).add(Restrictions.like("membername",search,MatchMode.ANYWHERE));
            }
            int records=this.memberRechargeTService.count(MemberRechargeT.class,criterion).intValue();
            searchModel.setRecordsTotal(records);
            searchModel.setRecordsFiltered(records);
            responsedata.put(TagWords.RECORDSTOTAL,records);
            responsedata.put(TagWords.RECORDSFILTERED,records);
            List<MemberRechargeT> list = this.memberRechargeTService.findByCriteriaByPage(
                    MemberRechargeT.class, criterion, Order.desc(TagWords.UPDATETIME), currentPage,
                    lineSize);
            this.processPageList(list);

        }else{
            if(StringUtils.isNotBlank(search)){
                filterSearch(searchModel,search);
            }else{
                findDefaultAll(searchModel,ordervalue,ordertype);
            }
        }
    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        criterion=Restrictions.and(criterion).add(Restrictions.like("membername",search,MatchMode.ANYWHERE));
        int records=this.memberRechargeTService.count(MemberRechargeT.class,criterion).intValue();
        searchModel.setRecordsTotal(records);
        searchModel.setRecordsFiltered(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        List<MemberRechargeT> list = this.memberRechargeTService.findByCriteriaByPage(
                MemberRechargeT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        int records=this.memberRechargeTService.count(MemberRechargeT.class,criterion).intValue();
        searchModel.setRecordsFiltered(records);
        searchModel.setRecordsTotal(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        Order order= BaseTools.orderByParam("membername",ordervalue,ordertype);
        List<MemberRechargeT> list = this.memberRechargeTService.findByCriteriaByPage(
                MemberRechargeT.class, criterion,order, currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void processPageList(List<MemberRechargeT> list) {
        List<Map<String,Object>>data=new ArrayList<>();
        for(MemberRechargeT bean:list){
            Map<String,Object>cellMap=new HashMap<>();
            cellMap.put("id",bean.getId());
            cellMap.put("memberid",bean.getMemberid());
            cellMap.put("membername",bean.getMembername());
            cellMap.put("balance",bean.getBalance());
            cellMap.put("status", BaseEnums.DataUsingState.getName(bean.getStatus()));
            cellMap.put("updatetime", DateTools.formateLongDateToString(bean.getUpdatetime().longValue()));
            cellMap.put("version", bean.getVersiont());
            data.add(cellMap);
        }
        responsedata.put(TagWords.DATA,data);
    }

    @Override
    public boolean checkDuplicates(String str) {
        return false;
    }
}
